/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hcsc.datapower.CMDB.daoImpl;

import com.hcsc.datapower.CMDB.dao.AbstractDao;
import com.hcsc.datapower.CMDB.dao.VendorDao;
import com.hcsc.datapower.CMDB.integration.Vendor;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.RowMapper;

/**
 *
 * @author I307211
 */
public class VendorDaoImpl extends AbstractDao implements VendorDao{
    
    private static final String TABLE = "Vendors";

    @Override
    public List<Vendor> getAllVendors() {
        String sql = "SELECT * FROM " + TABLE ;
        List<Vendor> tmp = this.jdbcTemplate.query(sql, new VendorMapper());
        
        if(tmp!=null)
            return tmp;
        return null;
    }

    @Override
    public Vendor getVendor(int id) {
        String sql = "SELECT * FROM " + TABLE + " WHERE id = ? ";
        return (Vendor) this.jdbcTemplate.query(sql, new Object[] { id }, new VendorMapper());
    }

    @Override
    public int updateVendor(Vendor vendor) {
            String sql = "UPDATE " + TABLE + " SET " +
            "id = ? " +
            "Name = ? " +
            "ConnType = ? " +
            "Direction = ? " +
            "LDAPUser = ? " +
            "TestPoolDestIp = ? " +
            "ProdPoolDestIp = ? " +
            "TestCertCNName = ? " +
            "TestCertFileName = ? " +
            "ProdCertCNName = ? " +
            "ProdCertFileName = ? " +
            "ContactName = ? " +
            "ContactPhone = ? " +
            "ContactEmail = ? " +
            "Comments = ? " +
            "WhoUpdated = ? " +
            "TimeUpdate = ? ";
            return jdbcTemplate.update(sql, new Object[] {
            vendor.getId(),
            vendor.getName(),
            vendor.getConnType(),
            vendor.getDirection(),
            vendor.getLDAPUser(),
            vendor.getTestPoolDestIp(),
            vendor.getProdPoolDestIp(),
            vendor.getTestCertCNName(),
            vendor.getTestCertFileName(),
            vendor.getProdCertCNName(),
            vendor.getProdCertFileName(),
            vendor.getContactName(),
            vendor.getContactPhone(),
            vendor.getContactEmail(),
            vendor.getComments(),
            vendor.getWhoUpdated(),
            vendor.getTimeUpdate()
            });
    }

    @Override
    public int deleteVendor(int id) {
        String sql = "DELETE FROM " + TABLE + " WHERE id = ? ";
        return jdbcTemplate.update(sql, new Object[] { id });
    }

    @Override
    public int createVendor(Vendor vendor) {
        String sql = "INSERT INTO " + TABLE +" Values ( ?, ? , ?, ?, ?, ? , ?, ?, ?, ? , ?, ?, ?, ? , ?, ?, ? ) ";
        return jdbcTemplate.update(sql, new Object[] {
            vendor.getId(),
            vendor.getName(),
            vendor.getConnType(),
            vendor.getDirection(),
            vendor.getLDAPUser(),
            vendor.getTestPoolDestIp(),
            vendor.getProdPoolDestIp(),
            vendor.getTestCertCNName(),
            vendor.getTestCertFileName(),
            vendor.getProdCertCNName(),
            vendor.getProdCertFileName(),
            vendor.getContactName(),
            vendor.getContactPhone(),
            vendor.getContactEmail(),
            vendor.getComments(),
            vendor.getWhoUpdated(),
            vendor.getTimeUpdate()
        });
    }
    
    private static final class VendorMapper implements RowMapper {

    public Object mapRow(ResultSet rs, int i) throws SQLException {
            Vendor vendor = new Vendor();
            vendor.setId(rs.getInt("id"));
            vendor.setName(rs.getString("Name"));
            vendor.setConnType(rs.getString("ConnType"));
            vendor.setDirection(rs.getString("Direction"));
            vendor.setLDAPUser(rs.getString("LDAPUser"));
            vendor.setTestPoolDestIp(rs.getString("TestPoolDestIp"));
            vendor.setProdPoolDestIp(rs.getString("ProdPoolDestIp"));
            vendor.setTestCertCNName(rs.getString("TestCertCNName"));
            vendor.setTestCertFileName(rs.getString("TestCertFileName"));
            vendor.setProdCertCNName(rs.getString("ProdCertCNName"));
            vendor.setProdCertFileName(rs.getString("ProdCertFileName"));
            vendor.setContactName(rs.getString("ContactName"));
            vendor.setContactPhone(rs.getString("ContactPhone"));
            vendor.setContactEmail(rs.getString("ContactEmail"));
            vendor.setComments(rs.getString("Comments"));
            vendor.setWhoUpdated(rs.getString("WhoUpdated"));
            vendor.setTimeUpdate(rs.getString("TimeUpdate"));
            return vendor;
        }
    }
}
